﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Web.Configuration;
using System.ServiceModel;
using BookClubIt.Processor;
using DTO = BookClubIt.DTO;

namespace BookClubIt
{
    public partial class members : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }

        }
        private void BindData()
        {
            DTO.BookClub club = BookClubProcessorInstance.GetBookClubById(CID);
            var club_Name = club.Name;
            // set page title
            Page.Title = club_Name + " members";
            lblClubName.Text = club_Name;

            string username = HttpContext.Current.User.Identity.Name;
            DTO.Member member = MemberProcessorInstance.GetMemberByUsername(username);
            int CurrentUserId = member.Id;

            //  imgUserAvatar.ImageURL = "";

            DTO.Members members = MemberProcessorInstance.GetBookClubMembersById(CID);
            grdMemberList.DataSource = members;
            grdMemberList.DataBind();

            if (BookClubProcessorInstance.IsLeader(member.Id, CID))
            {
                
                DTO.Members pendingMembers = MemberProcessorInstance.GetPendingBookClubMembersById(CID);

                if (pendingMembers.Count == 0)
                {
                    pnlPendingMembers.Visible = false;
                    grdPendingMembers.Visible = false;
                }
                else
                {
                    pnlPendingMembers.Visible = true;
                    grdPendingMembers.Visible = true;
                    grdPendingMembers.DataSource = pendingMembers;
                    grdPendingMembers.DataBind();
                }
                

            }
            if (BookClubProcessorInstance.IsLeader(CurrentUserId, CID))
            {
                // btnRemoveMember.Visible = true;

            }
        }

        public void BindData(DTO.Topics topics)
        {
            grdMemberList.DataSource = topics;
            grdMemberList.DataBind();
        }

        protected void grdMemberList_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            DTO.Member member = e.Row.DataItem as DTO.Member;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgUserAvatar = ((Image)e.Row.FindControl("imgUserAvatar"));
                HyperLink lnkUserProfile = ((HyperLink)e.Row.FindControl("lnkUserProfile"));
                Label lblJoinClubDate = ((Label)e.Row.FindControl("lblJoinClubDate"));
                LinkButton lnkMakeLeader = ((LinkButton)e.Row.FindControl("lnkMakeLeader"));
                LinkButton lnkRemoveMember = ((LinkButton)e.Row.FindControl("lnkRemoveMember"));
                LinkButton lnkRemoveLeader = ((LinkButton)e.Row.FindControl("lnkRemoveLeader"));
                HiddenField hdnRowUid = ((HiddenField)e.Row.FindControl("hdnRowUid"));
                Label lblMemberStatus = ((Label)e.Row.FindControl("lblMemberStatus"));

               
                lnkMakeLeader.CommandArgument = e.Row.RowIndex.ToString();

                imgUserAvatar.ImageUrl = "../avatars/" + member.AvatarURL;
                lnkUserProfile.NavigateUrl = "../member.aspx?" + member.Id;
                lnkUserProfile.Text = member.Username;
                lblJoinClubDate.Text = "Member since " + member.CreatedDate.ToString("MMM dd, yyyy");
                // get status
                if (BookClubProcessorInstance.IsAdmin(member.Id, CID))
                {
                    lblMemberStatus.Text = "Admin";
                }
                else if (BookClubProcessorInstance.IsLeader(member.Id, CID))
                {
                    lblMemberStatus.Text = "Leader";
                }
                else
                {
                    lblMemberStatus.Text = "Member";
                }
                // if current user is admin show all buttons
                if (BookClubProcessorInstance.IsAdmin(CurrentUserId, CID))
                {
                    lnkMakeLeader.Visible = true;
                    lnkRemoveMember.Visible = true;
                }
                // if current user is a leader but not an admin show remove member but not make leader
                else if (BookClubProcessorInstance.IsLeader(CurrentUserId, CID) && !BookClubProcessorInstance.IsAdmin(CurrentUserId, CID))
                {
                    lnkMakeLeader.Visible = false;
                    lnkRemoveMember.Visible = true;

                }
                // if its the current member dont show any buttons
                if (member.Id == CurrentUserId)
                {
                    lnkMakeLeader.Visible = false;
                    lnkRemoveMember.Visible = false;
                    lnkRemoveLeader.Visible = false;
                }

                else if (BookClubProcessorInstance.IsLeader(member.Id, CID) && BookClubProcessorInstance.IsAdmin(CurrentUserId, CID))
                {
                    lnkMakeLeader.Visible = false;
                    lnkRemoveMember.Visible = true;
                    lnkRemoveLeader.Visible = true;
                }

                hdnRowUid.Value = member.Id.ToString();
            }
        }

        protected void grdMemberList_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowNum = Convert.ToInt32(e.CommandArgument);
            HiddenField hdnRowUid = (grdMemberList.Rows[rowNum].FindControl("hdnRowUid")) as HiddenField;

            if (e.CommandName == "MakeLeader")
            {
                if (hdnRowUid != null)
                {
                    BookClubProcessorInstance.AddLeaderToClub(CID, Convert.ToInt32(hdnRowUid.Value));
                    BindData();
                }
            }

            if (e.CommandName == "RemoveMember")
            {
                // if member being removed is not a leader
                if (hdnRowUid != null && !BookClubProcessorInstance.IsLeader(Convert.ToInt32(hdnRowUid.Value), CID))
                {
                    BookClubProcessorInstance.RemoveMemberFromClub(CID, Convert.ToInt32(hdnRowUid.Value));
                    BindData();
                }
            }
            if (e.CommandName == "RemoveLeader")
            {
                // if member being removed is not a leader
                if (hdnRowUid != null && !BookClubProcessorInstance.IsAdmin(Convert.ToInt32(hdnRowUid.Value), CID))
                {
                   BookClubProcessorInstance.RemoveLeaderFromClub(CID, Convert.ToInt32(hdnRowUid.Value));
                    BindData();
                }
            }

        }

        protected void grdPendingMembers_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            DTO.Member pendingMembers = e.Row.DataItem as DTO.Member;

            if (e.Row.RowType != DataControlRowType.DataRow)
                return;

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Image imgUserAvatar = ((Image)e.Row.FindControl("imgUserAvatar"));
                HyperLink lnkUserProfile = ((HyperLink)e.Row.FindControl("lnkUserProfile"));
                LinkButton lnkApproveMember = ((LinkButton)e.Row.FindControl("lnkApproveMember"));
                LinkButton lnkDenyMember = ((LinkButton)e.Row.FindControl("lnkDenyMember"));
                HiddenField hdnPendingRowUid = ((HiddenField)e.Row.FindControl("hdnPendingRowUid"));

                lnkApproveMember.CommandArgument = e.Row.RowIndex.ToString();
                lnkDenyMember.CommandArgument = e.Row.RowIndex.ToString();

                imgUserAvatar.ImageUrl = "../avatars/" + pendingMembers.AvatarURL;
                lnkUserProfile.NavigateUrl = "../member.aspx?" + pendingMembers.Id;
                lnkUserProfile.Text = pendingMembers.Username;

                hdnPendingRowUid.Value = pendingMembers.Id.ToString();
            }
        }

        protected void grdPendingMembers_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int rowNum = Convert.ToInt32(e.CommandArgument);
            HiddenField hdnPendingRowUid = (grdPendingMembers.Rows[rowNum].FindControl("hdnPendingRowUid")) as HiddenField;

            if (e.CommandName == "ApproveMember")
            {
                if (hdnPendingRowUid != null)
                {
                    BookClubProcessorInstance.ApprovePendingMember(CID, Convert.ToInt32(hdnPendingRowUid.Value));
                    BookClubProcessorInstance.AddClubActivity(CID, Convert.ToInt32(hdnPendingRowUid.Value), "JOINED", CID);
                    // send email to user that they have been approved for the book club.
                    BindData();
                }
            }
            if (e.CommandName == "DenyMember")
            {
                if (hdnPendingRowUid != null)
                {
                    BookClubProcessorInstance.DenyPendingMember(CID, Convert.ToInt32(hdnPendingRowUid.Value));
                    // send email to user that they have been denied from club.
                    BindData();
                }
            }


        }

      
    }
}